package com.zy.sort;

import java.util.List;

/**
 * @description: 插入排序
 * @Author zhaoyi
 * @date 2019/11/6
 */
public class InsertSort<T extends Comparable<? super T>> {


    public void sort(List<T> list) {
        if (list == null || list.size() <= 1) {
            return;
        }
        for (int i = 1; i < list.size(); i++) {
            for (int j = (i - 1); j >= 0; j--) {
                T t = list.get(j + 1);
                T tj = list.get(j);
                if (tj.compareTo(t) > 0) {
                    list.set(j + 1, tj);
                    list.set(j, t);
                } else {
                    break;
                }
            }
        }
    }

}
